package com.woniuxy.course.controller;

import com.woniuxy.commons.dto.CourseDto;
import com.woniuxy.commons.dto.CoursewareDto;
import com.woniuxy.commons.entity.Courseware;
import com.woniuxy.commons.entity.PageBean;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.utils.CodeUtils;
import com.woniuxy.course.service.CoursewareService;
import com.woniuxy.course.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 王光强
 * @date 2020/7/21 0021-19:12
 */
@RestController
@RequestMapping("/courseware")
public class coursewareController {

    @Autowired
    private CoursewareService coursewareService;

    /**
     * @return 查询所有课件
     * @Author 王光强
     */
    @RequestMapping("/all")
    public ResponseEntity findAllCourseWare() {
        ResponseEntity<List<Courseware>> listResponseEntity = new ResponseEntity<>();
        List<Courseware> Courseware = coursewareService.findAll();
        if (Courseware != null) {
            return listResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setData(Courseware)
                    .setMessage("这是所有的课件类别");
        }
        return listResponseEntity.setMessage("没有任何数据，是不是出了什么问题？").setCode(StatusCode.RESPONSE_NULL);
    }

    /**
     * @return 通过cwid查询课件
     * @Author 王光强
     */
    @RequestMapping("/findCoursewareByCwid")
    public ResponseEntity findCoursewareByCwid(@RequestBody Courseware courseware) {
        System.out.println(courseware.getCwid());
        ResponseEntity<Courseware> listResponseEntity = new ResponseEntity<>();
        Courseware Courseware = coursewareService.findCoursewareByCwid(courseware.getCwid());
        if (Courseware != null) {
            return listResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setData(Courseware)
                    .setMessage("这是课件数据");
        }
        return listResponseEntity.setMessage("没有任何数据，是不是出了什么问题？").setCode(StatusCode.RESPONSE_NULL);
    }

    /**
     * @Author 王光强
     * @param coursewareDto
     * @return
     * 增加课件
     */
    @RequestMapping("/addCourseware")
    @ResponseBody
    public ResponseEntity<Void> addCourseware(@RequestBody CoursewareDto coursewareDto) {
        System.out.println("课件" + coursewareDto);
        ResponseEntity<Void> voidResponseEntity = new ResponseEntity<>();
        String[] names = coursewareDto.getCwnames();
        if (names == null || names.length == 0) {
            return voidResponseEntity.setCode(StatusCode.SERVER_ERROR).setMessage("未上传任何课件");
        }
        Courseware courseware = new Courseware();
        courseware.setDescription(coursewareDto.getDescription());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        courseware.setUpdateTime(df.format(new Date()));//时间
        courseware.setCategory(coursewareDto.getCategory());
        courseware.setIsOpen(coursewareDto.getIsOpen());
        if (coursewareDto.getCover() == null) {
            courseware.setCover("cover.jpg");
        } else {
            courseware.setCover(coursewareDto.getCover());
        }
        courseware.setDuration(coursewareDto.getDuration());
        courseware.setFormat(coursewareDto.getFormat());

        courseware.setIsDownload(coursewareDto.getIsDownload());
        System.out.println("-----------" + courseware);
        for (int i = 0; i < names.length; i++) {
            String fileName = names[i];
            String Name = fileName.substring(0, fileName.lastIndexOf(".")); //文件前缀
            String fileTyle = fileName.substring(fileName.lastIndexOf(".")+1);//文件后缀
            courseware.setCwname(Name);
            courseware.setFormat(fileTyle);
            try {
                int code = coursewareService.addCourseware(courseware);
                System.out.println("课件： " + courseware);
                if (code != StatusCode.OPERATION_SUCCESS) {
                    return voidResponseEntity.setCode(StatusCode.SERVER_ERROR).setMessage("系统异常");
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
        return voidResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setMessage("添加成功");
    }


    /**
     * @param
     * @return 通过课件id删除课件
     * @Author 王光强
     */
    @RequestMapping("/delCoursewareByCwid")
    public ResponseEntity<Void> delCoursewareByCwid(@RequestBody Courseware courseware) {
        int cwid = courseware.getCwid();
        ResponseEntity<Void> voidResponseEntity = new ResponseEntity<>();
        if (cwid == 0) {
            return voidResponseEntity.setCode(StatusCode.RESPONSE_NULL).setMessage("传参数出了问题");
        }
        if (coursewareService.delCoursewareByCwid(cwid) == StatusCode.OPERATION_SUCCESS) {
            return voidResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setMessage("删除成功");
        }
        return voidResponseEntity.setCode(StatusCode.SERVER_ERROR).setMessage("系统异常");
    }


    /**
     * @param
     * @return 通过课件多id批量删除课件
     * @Author 王光强
     */

    @RequestMapping("/delCoursewareByAnyCwid")
    public ResponseEntity<Void> delCoursewareByAnyCwid(@RequestBody CoursewareDto coursewareDto) {
        int cwids[] = coursewareDto.getCwids();
        ResponseEntity<Void> voidResponseEntity = new ResponseEntity<>();
        if (cwids == null) {
            return voidResponseEntity.setCode(StatusCode.RESPONSE_NULL).setMessage("传参数出了问题");
        }
        if (coursewareService.delCoursewareByAnyCwid(cwids) == StatusCode.OPERATION_SUCCESS) {
            return voidResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setMessage("删除成功");
        }
        return voidResponseEntity.setCode(StatusCode.SERVER_ERROR).setMessage("系统异常");
    }


    @RequestMapping("/update")
    public ResponseEntity<Void> updateCoursewareByCwid(@RequestBody Courseware courseware) {
        ResponseEntity<Void> voidResponseEntity = new ResponseEntity<>();
        if (courseware == null) {
            return voidResponseEntity.setCode(StatusCode.RESPONSE_NULL).setMessage("传参数出了问题");
        }
        if (coursewareService.updateCoursewareByCwid(courseware) == StatusCode.OPERATION_SUCCESS) {
            return voidResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setMessage("修改成功");
        }
        return voidResponseEntity.setCode(StatusCode.SERVER_ERROR).setMessage("系统异常");
    }


    @RequestMapping("/condition")
    public ResponseEntity<PageBean<CoursewareDto>> findCoursewareByCondition(@RequestBody CoursewareDto condition) {
        System.out.println(condition);
        ResponseEntity<PageBean<CoursewareDto>> listResponseEntity = new ResponseEntity<>();
        if (condition == null) {
            return listResponseEntity.setCode(StatusCode.RESPONSE_NULL).setMessage("有人在非法传参");
        }
        PageBean<CoursewareDto> coursewareByCondition = coursewareService.findCoursewareByCondition(condition);
        if (coursewareByCondition == null) {
            return listResponseEntity.setCode(StatusCode.RESPONSE_NULL).setMessage("系统异常");
        }
        return listResponseEntity.setMessage("课件查询结果").setCode(CodeUtils.SUCCESS_CODE)
                .setData(coursewareByCondition);
    }
}
